/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
 * Description: dev.h
 * Create: 2020-01-01
 */

#ifndef CCE_RUNTIME_RT_EXTERNAL_DEVICE_H
#define CCE_RUNTIME_RT_EXTERNAL_DEVICE_H

#include "rt_external_base.h"

#if defined(__cplusplus)
extern "C" {
#endif

/**
 * @ingroup dvrt_dev
 * @brief set chipType
 * @return RT_ERROR_NONE for ok
 */
RTS_API rtError_t rtSetSocVersion(const char_t *ver);

/**
* @ingroup
* @brief set debug dump mode
* @param [in] mode    : dump mode
* @return RT_ERROR_NONE for ok
* @return RT_ERROR_INVALID_VALUE for error input
*/
RTS_API rtError_t rtDebugSetDumpMode(const uint64_t mode);

typedef struct tagRtDbgCoreInfo {
	uint64_t aicBitmap0;
    uint64_t aicBitmap1;
	uint64_t aivBitmap0;
	uint64_t aivBitmap1;
} rtDbgCoreInfo_t;

/**
* @ingroup
* @brief get stalled core id in current process
* @param [out] coreInfo    : physics core id used
* @return RT_ERROR_NONE for ok
* @return RT_ERROR_INVALID_VALUE for error input
*/
RTS_API rtError_t rtDebugGetStalledCore(rtDbgCoreInfo_t *const coreInfo);

/**
 * @ingroup dvrt_dev
 * @brief get total device number.
 * @param
 * @return RT_ERROR_NONE for ok
 * @return RT_ERROR_INVALID_VALUE for error input
 */
RTS_API rtError_t rtInit(void);

/**
 * @ingroup dvrt_dev
 * @brief get total device number.
 * @param
 * @return RT_ERROR_NONE for ok
 * @return RT_ERROR_INVALID_VALUE for error input
 */
RTS_API void rtDeinit(void);

typedef enum {
    RT_RES_TYPE_STARS_NOTIFY_RECORD = 0,
    RT_RES_TYPE_STARS_CNT_NOTIFY_RECORD,
    RT_RES_TYPE_STARS_RTSQ,
    RT_RES_TYPE_CCU_CKE,
    RT_RES_TYPE_CCU_XN,
    RT_RES_TYPE_STARS_CNT_NOTIFY_BIT_WR,
    RT_RES_TYPE_STARS_CNT_NOTIFY_ADD,
    RT_RES_TYPE_STARS_CNT_NOTIFY_BIT_CLR,
    RT_RES_TYPE_MAX
} rtDevResType_t;

typedef enum {
    RT_PROCESS_CP1 = 0,    /* aicpu_scheduler */
    RT_PROCESS_CP2,        /* custom_process */
    RT_PROCESS_DEV_ONLY,   /* TDT */
    RT_PROCESS_QS,         /* queue_scheduler */
    RT_PROCESS_HCCP,       /* hccp server */
    RT_PROCESS_USER,       /* user proc, can bind many on host or device. not surport quert from host pid */
    RT_PROCESS_CPTYPE_MAX
} rtDevResProcType_t;

typedef enum {
    RT_DEV_STATUS_INITING = 0x0,
    RT_DEV_STATUS_WORK,
    RT_DEV_STATUS_EXCEPTION,
    RT_DEV_STATUS_SLEEP,
    RT_DEV_STATUS_COMMUNICATION_LOST,
    RT_DEV_STATUS_RESERVED
} rtDevStatus_t;

typedef struct {
    uint32_t dieId;  // for ccu res need set devId, for others set 0
    rtDevResProcType_t procType;
    rtDevResType_t resType;
    uint32_t resId;
    uint32_t flag;
} rtDevResInfo;

typedef struct {
    uint64_t *resAddress;
    uint32_t *len;
} rtDevResAddrInfo;

/**
 * @ingroup
 * @brief map resource va address
 * @param [in] resInfo resource info
 * @param [out] addrInfo resource address info
 * @return RT_ERROR_NONE for ok, errno for failed
 * @return RT_ERROR_INVALID_VALUE for error input
 */
RTS_API rtError_t rtGetDevResAddress(rtDevResInfo * const resInfo, rtDevResAddrInfo * const addrInfo);

/**
 * @ingroup
 * @brief unmap resource va address
 * @param [in] resInfo resource info
 * @param [out] resAddress resource address
 * @return RT_ERROR_NONE for ok, errno for failed
 * @return RT_ERROR_INVALID_VALUE for error input
 */
RTS_API rtError_t rtReleaseDevResAddress(rtDevResInfo * const resInfo);

typedef enum {
    QUERY_PROCESS_TOKEN,
    QUERY_TYPE_BUFF
} rtUbDevQueryCmd;

/**
 * @ingroup
 * @brief query ub device info
 * @param [in] cmd query info tpye
 * @param [in|out] info input/output parameter
 * @return RT_ERROR_NONE for ok, errno for failed
 * @return RT_ERROR_INVALID_VALUE for error input
 */
RTS_API rtError_t rtUbDevQueryInfo(rtUbDevQueryCmd cmd, void *devInfo);

// 拉起HCCP进程
struct rtProcExtParam {
    const char  *paramInfo;
    uint64_t    paramLen;
};

struct rtNetServiceOpenArgs {
    rtProcExtParam *extParamList;   // 拉起服务的参数列表
    uint64_t     extParamCnt;    // 拉起服务的参数列表长度
};

#define RT_EXT_PARAM_CNT_MAX  127U

/**
 * @ingroup
 * @brief Open NetService for HCCL
 * @param [in] args   service args
 * @return RT_ERROR_NONE for ok, errno for failed
 */
RTS_API rtError_t rtOpenNetService(const rtNetServiceOpenArgs *args);

/**
 * @ingroup
 * @brief Close NetService for HCCL
 * @return RT_ERROR_NONE for ok, errno for failed
 */
RTS_API rtError_t rtCloseNetService();

/**
 * @ingroup dvrt_dev
 * @brief set target device for current thread
 * @param [int] devId   the device id
 * @return RT_ERROR_NONE for ok
 * @return RT_ERROR_INVALID_VALUE for error input
 */
RTS_API rtError_t rtSetDeviceWithoutTsd(int32_t devId);

// used for rtGetDevMsg callback function
typedef void (*rtGetMsgCallback)(const char_t *msg, uint32_t len);

typedef enum tagGetDevMsgType {
    RT_GET_DEV_ERROR_MSG = 0,
    RT_GET_DEV_RUNNING_STREAM_SNAPSHOT_MSG,
    RT_GET_DEV_PID_SNAPSHOT_MSG,
    RT_GET_DEV_MSG_RESERVE
} rtGetDevMsgType_t;

/**
 * @ingroup dvrt_dev
 * @brief get device message
 * @param [in] rtGetDevMsgType_t getMsgType:msg type
 * @param [in] GetMsgCallback callback:acl callback function
 * @return RT_ERROR_NONE for ok
 * @return RT_ERROR_INVALID_VALUE for error input
 */
RTS_API rtError_t rtGetDevMsg(rtGetDevMsgType_t getMsgType, rtGetMsgCallback callback);

#if defined(__cplusplus)
}
#endif

#endif  // CCE_RUNTIME_RT_EXTERNAL_DEVICE_H